import Taro from '@tarojs/taro'

import {
	MiniCodeForm,
	PhoneForm,
	SaTokenInfo,
	SendCodeOption,
	SendCodeResult
} from '../../common/types/interface'
import { SmsTemplateCode } from '../../common/enums/sms'

const sendSmsCodePath = 'https://api.test.eastcoal.cn/nexus/sms/code/send'
export const sendSmsCodeApi = (params: SendCodeOption) => {
	return new Promise<SendCodeResult>((resolove, reject) => [
		Taro.request({
			url: `${sendSmsCodePath}`,
			data: { ...params, digit: 4, templateCode: SmsTemplateCode.LOGIN },
			method: 'POST',
			success(res) {
				resolove(res.data)
			},
			fail(res) {
				reject(res)
			}
		})
	])
}

const loginByPhonePath = 'https://api.test.eastcoal.cn/login/miniPhone'
export const loginByPhoneApi = (params: PhoneForm) => {
	return new Promise<SaTokenInfo>((resolove, reject) => [
		Taro.request({
			url: `${loginByPhonePath}`,
			data: params,
			method: 'POST',
			success(res) {
				resolove(res.data)
			},
			fail(res) {
				reject(res)
			}
		})
	])
}
const loginByMiniCodePath = 'https://api.test.eastcoal.cn/auth/login/miniCode'
export const loginByMiniCodeApi = (params: MiniCodeForm) => {
	return new Promise<SaTokenInfo>((resolove, reject) => [
		Taro.request({
			url: `${loginByMiniCodePath}`,
			data: params,
			method: 'POST',
			success(res) {
				resolove(res.data)
			},
			fail(res) {
				reject(res)
			}
		})
	])
}
